package com.joinway.framework.admin.core.utils;

import javax.servlet.http.HttpServletRequest;

import com.joinway.framework.admin.bean.UserContext;
import com.joinway.framework.bean.exception.SessionExpiredException;
import com.joinway.framework.core.utils.FrameworkHelper;

@Deprecated
public final class SessionHelper {

	static final String UC = "com.joinway.user.context";
	
	public static UserContext getUserContext() throws SessionExpiredException {
		return getUserContext(false);
	}
	
	public static UserContext getUserContext(boolean create) throws SessionExpiredException {
		HttpServletRequest request = FrameworkHelper.getHttpServletRequest();
		
		UserContext uc = (UserContext)request.getSession().getAttribute(UC);
		if(uc == null && create){
			uc = new UserContext();
			
			request.getSession().setAttribute(UC, uc);
		}else if(uc == null && !create){
			throw new SessionExpiredException("session was expired");
		}
		
		return uc;
	}

	public static void setUserContext(UserContext context){
		HttpServletRequest request = FrameworkHelper.getHttpServletRequest();
		request.getSession().setAttribute(UC, context);
	}
	
	private SessionHelper(){}
}
